<?php
/** INSCRIPTION **/
?>
<div class="manage_section">
	
	<h2>Nouvelles demandes</h2>
	
	<div id="waiting_div">
	
		<?php 
		if(empty($this->waiting_users['waiting'])) {
		?>
			<div class="information">Aucun utilisateur en attente dans cette section</div>
		<?php
		} else {
			?>
			<form id="waiting_form" action="<?php echo $this->url(array('module' => 'backend', 'controller' => 'manage', 'action' => 'manageapply'), null, true); ?>" method="post">
			<?php foreach($this->waiting_users['waiting'] AS $echelon => $s) { ?>
				<div class="structure_name">
					<h4><?php echo $echelon; ?></h4>
				</div>	

				<table id="waiting_tab">
					<tr>
						<th>Date de la demande</th>
						<th>Fonction</th>
						<th>Civilit&eacute;</th>
						<th>Nom</th>
						<?php if($this->action == "archivated"){?>
						<th>Statut</th>
						<th>Modifier</th>
						<?php }else{ } ?>
						<th>Accepter</th>
						<th>Refuser</th>
						<th>En attente</th>
					</tr>
					
					<?php foreach($s['user'] AS $u) {
						// Change function format
						$function = preg_replace('/___/', ' ', $u['function']);
						$function = preg_replace('/_/', ' ', $u['function']);	// For Animateur_BDI
						?>
						<tr>
							<td><?php echo $u['date']; ?></td>
							<td><?php echo $function; ?></td>
							<td><?php echo $u['extra']['civility']; ?></td>
							<td style="cursor: pointer;" onclick="javascript: window.open('<?php echo $this->url(array('module' => 'backend', 'controller' => 'user', 'action' => 'set', 'id' => $u['id_user']), null, true);?>', 'Fiche_utilisateur', 'height=800, width=600, left=50%, toolbar=no, menubar=no, location=no, resizable=no, scrollbars=yes, status=no')" title="modifier l'utilisateur"><?php echo $u['lastname'].' '.$u['firstname']; ?></td>
							<td><input type="radio" name="status_waiting_<?php echo $u['id']; ?>" value="accepted_<?php echo $u['id_user'].'_'.$s['structure']['id'].'_'.$u['icf']; ?>" /></td>
							<td><input type="radio" name="status_waiting_<?php echo $u['id']; ?>" value="rejected_<?php echo $u['id_user'].'_'.$s['structure']['id'].'_'.$u['icf']; ?>" /></td>
							<td><input type="radio" name="status_waiting_<?php echo $u['id']; ?>" value="waiting_<?php echo $u['id_user'].'_'.$s['structure']['id'].'_'.$u['icf']; ?>" checked="checked" /></td>
						</tr>
					<?php } ?>
				</table>
			<?php
			}
			?>
			</form>
			<input type="hidden" name="management" value="waiting" />
			<div id="waiting_mess" style="display: none;"></div>
			<input type="submit" id="waiting_send" class="form_submit" name="manage_send" value="Confirmer" />
		<?php
		}
		?>
	</div>
</div>

<div class="manage_section">
	
	<h2>Demandes de renouvellement</h2>
	
	<div id="renewall_div">
		
		<?php 
		if(!isset($this->waiting_users['renewall'])) {
		?>
			<div class="information">Aucun utilisateur en attente dans cette section</div>
		<?php
		} else {
			?>
			<form id="renewall_form" action="<?php echo $this->url(array('module' => 'backend', 'controller' => 'manage', 'action' => 'manageapply', 'format' => 'json'), null, true); ?>" method="post">
			<?php foreach($this->waiting_users['renewall'] AS $echelon => $s) { ?>
				<div class="structure_name">
					<h4><?php echo $echelon; ?></h4>
				</div>	

				<table id="renewall_tab">
					<tr>
						<th>Date de la demande</th>
						<th style="width: 350px;">Fonction</th>
						<th>Civilit&eacute;</th>
						<th>Nom
						<?php if($this->action == "archivated"){?>
						<th>Statut</th>
						<th>Modifier</th>
						<?php }else{ } ?>
						<th>Accepter</th>
						<th>Refuser</th>
						<th>En attente</th>
					</tr>

					<?php foreach($s['user'] AS $u) {
						// Change function format
						$function = preg_replace('/___/', ' ', $u['function']);
						$function = preg_replace('/_/', ' ', $u['function']);	// For Animateur_BDI
						?>
						<tr>
							<td><?php echo $u['date']; ?></td>
							<td><?php echo $function; ?></td>
							<td><?php echo $u['extra']['civility']; ?></td>
							<td style="cursor: pointer;" onclick="javascript: window.open('<?php echo $this->url(array('module' => 'backend', 'controller' => 'user', 'action' => 'set', 'id' => $u['id_user'], 'for' => 'renewall'), null, true);?>', 'Fiche_utilisateur', 'height=800, width=600, left=50%, toolbar=no, menubar=no, location=no, resizable=no, scrollbars=yes, status=no')" title="modifier l'utilisateur"><?php echo $u['lastname'].' '.$u['firstname']; ?></td>
							<td><input type="radio" name="status_renewall_<?php echo $u['id']; ?>" value="accepted_<?php echo $u['id_user'].'_'.$s['structure']['id'].'_'.$u['icf']; ?>" /></td>
							<td><input type="radio" name="status_renewall_<?php echo $u['id']; ?>" value="rejected_<?php echo $u['id_user'].'_'.$s['structure']['id'].'_'.$u['icf']; ?>" /></td>
							<td><input type="radio" name="status_renewall_<?php echo $u['id']; ?>" value="waiting_<?php echo $u['id_user'].'_'.$s['structure']['id'].'_'.$u['icf']; ?>" checked="checked" /></td>
						</tr>
					<?php } ?>
				</table>
			<?php
			}
			?>
			</form>
		<input type="hidden" name="management" value="renewall" />
		<div id="renewall_mess" style="display: none;"></div>
		<input type="submit" id="renewall_send" class="form_submit" name="manage_send" value="Confirmer" />
		<?php
		}
		?>
	</div>
</div>

<div class="manage_section">
	
	<h2>Archives <img src="<?php echo $this->baseUrl; ?>/images/icones/zoom.png" alt="Voir" class="show_logs" style="cursor: pointer;" /></h2>
	
	<div id="logs_div">
		
	</div>
	
</div>

<script type="text/javascript">

	$('.form_submit').click(function(e) {
		// Get management
		var id = $(this).attr('id');
		id_tab = id.split('_');
		var management = id_tab[0];
		
		$('#'+management+'_mess').html('<img src="<?php echo $this->baseUrl; ?>/images/icones/loading.gif" alt="" />');
		$('#'+management+'_mess').fadeIn();
		
		$('#'+management+'_form').ajaxSubmit({
			dataType: 'json',
			success: function(e) {
				if(e.return != 'OK') {
					$('#'+management+'_mess').html('<div class="error">'+e.return+'</div>');
				
				} else {
					$('#'+management+'_mess').html('<div class="information">Traitement effectué</div>');
					$('#'+management+'_div').fadeOut();
					setTimeout('location.reload()', 2000);
				}
				return false;
			}
		});
		
		return false;
		
	});
	
	$('.show_logs').click(function(e) {
		
		$('#logs_div').html('<img src="<?php echo $this->baseUrl; ?>/images/icones/loading.gif" alt="" /> Chargement de vos archives ... (Peut prendre de quelques secondes à plusieurs minutes)');
		
		$.ajax({
			url: '<?php echo $this->url(array('module' => 'backend', 'controller' => 'manage', 'action' => 'logs'), null, true); ?>',
			success: function(e) {
				$('#logs_div').html(e);
			}
		});
		
	});
	
</script>
